package org.samith.web.ws.service;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

import org.samith.dao.ServiceTypeDao;
import org.samith.dao.SubscribedServiceDao;
import org.samith.dao.SubscriberDao;
import org.samith.dao.TransactionDao;
import org.samith.entity.ServiceType;
import org.samith.entity.SubscribedService;
import org.samith.entity.Subscriber;
import org.samith.entity.Transaction;

@WebService(endpointInterface = "org.samith.web.ws.service.IServiceActivation", serviceName = "WSMobileActivation")
public class UserServiceActivation implements IServiceActivation {

	@EJB
	ServiceTypeDao serviceTypeDao;
	@EJB
	SubscriberDao subscriberDao;
	@EJB
	SubscribedServiceDao subscribedServiceDao;
	@EJB
	TransactionDao transactionDao;

	UserTransaction tx;

	@Override
	public boolean activateUserForService(int userId, int serviceId) {
		Context ctx = null;
		try {
			ctx = new InitialContext();
		} catch (NamingException e2) {
			e2.printStackTrace();
		}
		try {
			tx = (UserTransaction) ctx.lookup("UserTransaction");
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Long userActId = (long) userId;
		Subscriber subscriber = subscriberDao.findByUserId(userActId);
		ServiceType toBeSubscribedService = serviceTypeDao.read(Long
				.valueOf(serviceId));
		boolean alreadySubscribed = subscribedServiceDao
				.hasSubscribedToService(toBeSubscribedService, subscriber);
		if (!alreadySubscribed) {
			SubscribedService subsService = new SubscribedService();
			subsService.setServiceId(toBeSubscribedService);
			subsService.setSubscriberId(subscriber);
			subsService.setIsActive(new Byte("1"));

			Transaction trn = new Transaction();
			trn.setServiceId(toBeSubscribedService);
			trn.setSubscriberId(subscriber);
			trn.setAmount(30.00);// Rs. 30.00 for any service activation
			trn.setTransactionCreatedAt(Calendar.getInstance().getTime());
			try {
				tx.begin();
				SubscribedService ss = subscribedServiceDao.create(subsService);
				Transaction t = transactionDao.create(trn);
				tx.commit();
				if (ss != null && t != null) {
					System.out
							.println(" *** New subscribed service saved ****** ");
					return true;
				}
				return false;
			} catch (Exception e) {
				System.out
						.println("============= Exception in service activation process");
				e.printStackTrace();
				return false;
			}
		} else {
			System.out
					.println(" *** New subscribed already in the list, Skipping.. ****** ");
			return true;
		}

	}

	@Override
	public boolean deActivateUserForService(int userId, int serviceId) {
		Context ctx = null;
		try {
			ctx = new InitialContext();
		} catch (NamingException e2) {
			e2.printStackTrace();
		}
		try {
			tx = (UserTransaction) ctx.lookup("UserTransaction");
		} catch (NamingException e1) {
			e1.printStackTrace();
		}
		Long userActId = (long) userId;
		try {
			tx.begin();
			Subscriber subscriber = subscriberDao.findByUserId(userActId);
			ServiceType toBeSubscribedService = serviceTypeDao.read(Long
					.valueOf(serviceId));
			SubscribedService subscribedService = subscribedServiceDao
					.getSubscribedToServiceRecord(toBeSubscribedService,
							subscriber);
			if (subscribedService != null) {
				subscribedServiceDao.delete(subscribedService);
				tx.commit();
				return true;
			}
		} catch (Exception e) {
			System.out
					.println("============= Exception in service deactivation process");
			e.printStackTrace();
			return false;
		}
		return false;
	}

}
